import { Init, Provide, Inject } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { AnchorDynamicTypeEntity } from '../entity/dynamic_type';
import { DemoUserInfoEntity } from '../../demo/entity/info';
import { MemberUserInfoEntity } from '../../member/entity/info';
import { AnchorDynamicEntity } from '../../anchorDynamic/entity/dynamic';


/**
 * anchorDynamic模块-动态信息服务
 */
@Provide()
export class AnchorDynamicTypeService extends BaseService {
  @InjectEntityModel(AnchorDynamicTypeEntity)
  anchorDynamicTypeEntity: Repository<AnchorDynamicTypeEntity>;

  @InjectEntityModel(DemoUserInfoEntity)
  demoUserInfoEntity: Repository<DemoUserInfoEntity>;

  @InjectEntityModel(MemberUserInfoEntity)
  memberUserInfoEntity: Repository<MemberUserInfoEntity>;


  @InjectEntityModel(AnchorDynamicEntity)
  anchorDynamicEntity: Repository<AnchorDynamicEntity>;
  
  @Init()
  async init() {
    await super.init();
    this.setEntity(this.anchorDynamicTypeEntity);
  }

  @Inject()
  ctx;

  async unLockActivity(BodyData: any) {
    let userId = this.ctx.header.token
    let userItem = await this.memberUserInfoEntity.findOneBy({id:Number(userId)})
    console.log(BodyData, userItem, 'BodyData')
    if(userItem.points < 5){
      throw new Error('用户金币不足');
    }
    let params = {
      dynamicId: BodyData.id,
      userId
    }
    let res = await this.anchorDynamicTypeEntity.save(params)
    let resImg = await this.anchorDynamicEntity.findOneBy({id:BodyData.id})
    await this.memberUserInfoEntity.update({id:Number(userId)},{points:userItem.points-5})
    return resImg.dynamicImages
  }

}
